package org.infodavid.common.services;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IOrganizationListItem;
import org.infodavid.common.impl.model.criteria.OrganizationCriteriaImpl;
import org.infodavid.common.model.AbstractBeanTest;
import org.infodavid.common.model.IOrganization;
import org.infodavid.common.model.OrganizationTest;
import org.infodavid.common.model.criteria.IOrganizationCriteria;
import org.infodavid.common.persistence.OrganizationDataService;
import org.infodavid.common.services.OrganizationService;

/**
 * The Class OrganizationServiceTest.
 */
@SuppressWarnings({
	"null"
})
public class OrganizationServiceTest
    extends
    AbstractServiceTest<IOrganizationListItem,IOrganization,IOrganizationCriteria,OrganizationDataService> {

	/** The delegate. */
	private final OrganizationTest delegate = new OrganizationTest(this);

	/**
	 * The Constructor.
	 */
	public OrganizationServiceTest() {
		super(OrganizationService.getInstance());
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @return the data service class
	 * @see org.infodavid.common.services.AbstractServiceTest#getDataServiceClass()
	 */
	@Override
	public Class<OrganizationDataService> getDataServiceClass() {
		return OrganizationDataService.class;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.services.AbstractServiceTest#buildCriteria()
	 */
	@Override
	public IOrganizationCriteria buildCriteria() {
		final IOrganizationCriteria criteria = new OrganizationCriteriaImpl();

		criteria.setNamePattern("name");

		return criteria;
	}

	/**
	 * Test find by name.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByName() throws Exception {
		final List<IOrganizationListItem> results = new ArrayList<IOrganizationListItem>();
		final IOrganization value = populate(ITEMS_TO_INSERT).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((OrganizationService)getService()).findByName(getContextMock(), value.getName(), results,
			    null);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNull("An exception occurs", exception);
		Assert.assertNotNull("Data is null", value);
		Assert.assertNotNull("Key is null", value.getKey());
	}

	/**
	 * Test testFindByName.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByNameWithoutValue() throws Exception {
		final List<IOrganizationListItem> results = new ArrayList<IOrganizationListItem>();
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((OrganizationService)getService()).findByName(getContextMock(), null, results, null);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/**
	 * Test testFindByName.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByNameUsingAnInvalidContext() throws Exception {
		final List<IOrganizationListItem> results = new ArrayList<IOrganizationListItem>();
		final IOrganization value = populate(ITEMS_TO_INSERT).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((OrganizationService)getService()).findByName(null, value.getName(), results, null);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/**
	 * Test add.
	 * @throws Exception the exception
	 */
	@Test
	public final void testAddWithoutName() throws Exception {
		final IOrganization value = create(1).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		value.setName(null);

		try {
			getService().add(getContextMock(), value);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/**
	 * Test get entities.
	 * @throws Exception the exception
	 */
	@Test
	public void testGetEntitiesWithCriteria() throws Exception {
		final IOrganization value = populate(ITEMS_TO_INSERT).get(0);
		final IOrganizationCriteria criteria = buildCriteria();
		final List<IOrganizationListItem> results = new ArrayList<IOrganizationListItem>();
		Exception exception = null;

		expectationsOnPersistenceSession();
		expectationsOnSecureService();

		try {
			getService().findEntities(getContextMock(), results, criteria);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNull("An exception occurs", exception);
		Assert.assertNotNull("Data is null", value);
		Assert.assertNotNull("Key is null", value.getKey());
	}

	/**
	 * Test get entities.
	 * @throws Exception the exception
	 */
	@Test
	public void testGetEntitiesWithCriteriaUsingAnInvalidContext() throws Exception {
		final IOrganizationCriteria criteria = buildCriteria();
		final List<IOrganizationListItem> results = new ArrayList<IOrganizationListItem>();
		Exception exception = null;

		expectationsOnPersistenceSession();
		expectationsOnSecureService();

		try {
			getService().findEntities(null, results, criteria);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/**
	 * Test get entities.
	 * @throws Exception the exception
	 */
	@Test
	public void testGetEntitiesWithCriteriaUsingAnInvalidCriteria() throws Exception {
		final IOrganization value = populate(ITEMS_TO_INSERT).get(0);
		final List<IOrganizationListItem> results = new ArrayList<IOrganizationListItem>();
		Exception exception = null;

		expectationsOnPersistenceSession();
		expectationsOnSecureService();

		try {
			getService().findEntities(getContextMock(), results, null);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNull("An exception occurs", exception);
		Assert.assertNotNull("Data is null", value);
		Assert.assertNotNull("Key is null", value.getKey());
	}

	/**
	 * Test get entities.
	 * @throws Exception the exception
	 */
	@Test
	public void testGetEntitiesWithCriteriaUsingAnInvalidList() throws Exception {
		populate(ITEMS_TO_INSERT).get(0);
		final IOrganizationCriteria criteria = buildCriteria();
		Exception exception = null;

		expectationsOnPersistenceSession();
		expectationsOnSecureService();

		try {
			getService().findEntities(getContextMock(), null, criteria);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.persistence.AbstractDataServiceTest#getBeanTest()
	 */
	@Override
	protected AbstractBeanTest<IOrganization> getBeanTest() {
		return delegate;
	}

}
